<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- Copyright (C) 1999-2023 Free Software Foundation, Inc.

Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3 or
any later version published by the Free Software Foundation; with the
Invariant Sections being "Funding Free Software", the Front-Cover
Texts being (a) (see below), and with the Back-Cover Texts being (b)
(see below).  A copy of the license is included in the section entitled
"GNU Free Documentation License".

(a) The FSF's Front-Cover Text is:

A GNU Manual

(b) The FSF's Back-Cover Text is:

You have freedom to copy and modify this GNU Manual, like GNU
     software.  Copies published by the Free Software Foundation raise
     funds for GNU development. -->
<!-- Created by GNU Texinfo 6.5, http://www.gnu.org/software/texinfo/ -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Data consistency and durability (The GNU Fortran Compiler)</title>

<meta name="description" content="Data consistency and durability (The GNU Fortran Compiler)">
<meta name="keywords" content="Data consistency and durability (The GNU Fortran Compiler)">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<link href="index.html#Top" rel="start" title="Top">
<link href="Option-Index.html#Option-Index" rel="index" title="Option Index">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="Compiler-Characteristics.html#Compiler-Characteristics" rel="up" title="Compiler Characteristics">
<link href="Files-opened-without-an-explicit-ACTION_003d-specifier.html#Files-opened-without-an-explicit-ACTION_003d-specifier" rel="next" title="Files opened without an explicit ACTION= specifier">
<link href="Thread_002dsafety-of-the-runtime-library.html#Thread_002dsafety-of-the-runtime-library" rel="prev" title="Thread-safety of the runtime library">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.indentedblock {margin-right: 0em}
blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
blockquote.smallquotation {font-size: smaller}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.lisp {margin-left: 3.2em}
div.smalldisplay {margin-left: 3.2em}
div.smallexample {margin-left: 3.2em}
div.smalllisp {margin-left: 3.2em}
kbd {font-style: oblique}
pre.display {font-family: inherit}
pre.format {font-family: inherit}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
pre.smalldisplay {font-family: inherit; font-size: smaller}
pre.smallexample {font-size: smaller}
pre.smallformat {font-family: inherit; font-size: smaller}
pre.smalllisp {font-size: smaller}
span.nolinebreak {white-space: nowrap}
span.roman {font-family: initial; font-weight: normal}
span.sansserif {font-family: sans-serif; font-weight: normal}
ul.no-bullet {list-style: none}
-->
</style>


</head>

<body lang="en">
<a name="Data-consistency-and-durability"></a>
<div class="header">
<p>
Next: <a href="Files-opened-without-an-explicit-ACTION_003d-specifier.html#Files-opened-without-an-explicit-ACTION_003d-specifier" accesskey="n" rel="next">Files opened without an explicit ACTION= specifier</a>, Previous: <a href="Thread_002dsafety-of-the-runtime-library.html#Thread_002dsafety-of-the-runtime-library" accesskey="p" rel="prev">Thread-safety of the runtime library</a>, Up: <a href="Compiler-Characteristics.html#Compiler-Characteristics" accesskey="u" rel="up">Compiler Characteristics</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html#Option-Index" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<a name="Data-consistency-and-durability-1"></a>
<h3 class="section">4.6 Data consistency and durability</h3>
<a name="index-consistency_002c-durability"></a>

<p>This section contains a brief overview of data and metadata
consistency and durability issues when doing I/O.
</p>
<p>With respect to durability, GNU Fortran makes no effort to ensure that
data is committed to stable storage. If this is required, the GNU
Fortran programmer can use the intrinsic <code>FNUM</code> to retrieve the
low level file descriptor corresponding to an open Fortran unit. Then,
using e.g. the <code>ISO_C_BINDING</code> feature, one can call the
underlying system call to flush dirty data to stable storage, such as
<code>fsync</code> on POSIX, <code>_commit</code> on MingW, or <code>fcntl(fd,
F_FULLSYNC, 0)</code> on Mac OS X. The following example shows how to call
fsync:
</p>
<div class="smallexample">
<pre class="smallexample">  ! Declare the interface for POSIX fsync function
  interface
    function fsync (fd) bind(c,name=&quot;fsync&quot;)
    use iso_c_binding, only: c_int
      integer(c_int), value :: fd
      integer(c_int) :: fsync
    end function fsync
  end interface

  ! Variable declaration
  integer :: ret

  ! Opening unit 10
  open (10,file=&quot;foo&quot;)

  ! ...
  ! Perform I/O on unit 10
  ! ...

  ! Flush and sync
  flush(10)
  ret = fsync(fnum(10))

  ! Handle possible error
  if (ret /= 0) stop &quot;Error calling FSYNC&quot;
</pre></div>

<p>With respect to consistency, for regular files GNU Fortran uses
buffered I/O in order to improve performance. This buffer is flushed
automatically when full and in some other situations, e.g. when
closing a unit. It can also be explicitly flushed with the
<code>FLUSH</code> statement. Also, the buffering can be turned off with the
<code>GFORTRAN_UNBUFFERED_ALL</code> and
<code>GFORTRAN_UNBUFFERED_PRECONNECTED</code> environment variables. Special
files, such as terminals and pipes, are always unbuffered. Sometimes,
however, further things may need to be done in order to allow other
processes to see data that GNU Fortran has written, as follows.
</p>
<p>The Windows platform supports a relaxed metadata consistency model,
where file metadata is written to the directory lazily. This means
that, for instance, the <code>dir</code> command can show a stale size for a
file. One can force a directory metadata update by closing the unit,
or by calling <code>_commit</code> on the file descriptor. Note, though,
that <code>_commit</code> will force all dirty data to stable storage, which
is often a very slow operation.
</p>
<p>The Network File System (NFS) implements a relaxed consistency model
called open-to-close consistency. Closing a file forces dirty data and
metadata to be flushed to the server, and opening a file forces the
client to contact the server in order to revalidate cached
data. <code>fsync</code> will also force a flush of dirty data and metadata
to the server. Similar to <code>open</code> and <code>close</code>, acquiring and
releasing <code>fcntl</code> file locks, if the server supports them, will
also force cache validation and flushing dirty data and metadata.
</p>

<hr>
<div class="header">
<p>
Next: <a href="Files-opened-without-an-explicit-ACTION_003d-specifier.html#Files-opened-without-an-explicit-ACTION_003d-specifier" accesskey="n" rel="next">Files opened without an explicit ACTION= specifier</a>, Previous: <a href="Thread_002dsafety-of-the-runtime-library.html#Thread_002dsafety-of-the-runtime-library" accesskey="p" rel="prev">Thread-safety of the runtime library</a>, Up: <a href="Compiler-Characteristics.html#Compiler-Characteristics" accesskey="u" rel="up">Compiler Characteristics</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html#Option-Index" title="Index" rel="index">Index</a>]</p>
</div>



</body>
</html>
